package main

import (
	"context"
	"encoding/json"
	"math/rand"
	pb "monthly/kitex_gen/monthly/passenger"
	"monthly/rpc/basic/global"
	"monthly/rpc/basic/model"
	"time"
)

// PassengerServiceImpl implements the last service interface defined in the IDL.
type PassengerServiceImpl struct{}

// SendSms implements the PassengerServiceImpl interface.
func (s *PassengerServiceImpl) SendSms(ctx context.Context, req *pb.SendSmsReq) (*pb.SendSmsResp, error) {
	// TODO: Your code here...
	ctx = context.Background()
	code := rand.Intn(9000) + 1000
	err := global.Rdb.Set(ctx, "sendSms"+req.Mobile, code, time.Minute*5).Err()
	if err != nil {
		return &pb.SendSmsResp{
			Code:    503,
			Message: "服务器异常",
		}, nil
	}
	return &pb.SendSmsResp{
		Code:    200,
		Message: "短信发送成功",
	}, nil
}

// Register implements the PassengerServiceImpl interface.
func (s *PassengerServiceImpl) Register(ctx context.Context, req *pb.RegisterReq) (*pb.RegisterResp, error) {
	ctx = context.Background()
	result, err := global.Rdb.Get(ctx, "sendSms"+req.Mobile).Result()
	if err != nil {
		return &pb.RegisterResp{
			Code:    400,
			Message: "短信验证码获取失败",
		}, nil
	}
	var passenger model.Passenger
	if err = global.DB.Debug().Where("mobile = ?", req.Mobile).Limit(1).Find(&passenger).Error; err != nil {
		return &pb.RegisterResp{
			Code:    503,
			Message: "查询失败",
		}, nil
	}
	if passenger.Id != 0 {
		return &pb.RegisterResp{
			Code:    401,
			Message: "用户已存在",
		}, nil
	}
	if result != req.SendSmsCode {
		return &pb.RegisterResp{
			Code:    301,
			Message: "验证码错误",
		}, nil
	}
	nikeName := "monthly" + req.Mobile
	m := model.Passenger{
		NikeName: nikeName,
		Mobile:   req.Mobile,
	}
	if err = global.DB.Debug().Create(&m).Error; err != nil {
		return &pb.RegisterResp{
			Code:    503,
			Message: "服务器异常,注册失败",
		}, nil
	}
	return &pb.RegisterResp{
		Code:    200,
		Message: "注册成功",
	}, nil
}

// Login implements the PassengerServiceImpl interface.
func (s *PassengerServiceImpl) Login(ctx context.Context, req *pb.LoginReq) (*pb.LoginResp, error) {
	// TODO: Your code here...
	ctx = context.Background()
	result, err := global.Rdb.Get(ctx, "sendSms"+req.Mobile).Result()
	if err != nil {
		return &pb.LoginResp{
			Code:    400,
			Message: "短信验证码获取失败",
		}, nil
	}
	var passenger model.Passenger
	if err = global.DB.Debug().Where("mobile = ?", req.Mobile).Limit(1).Find(&passenger).Error; err != nil {
		return &pb.LoginResp{
			Code:    503,
			Message: "查询失败",
		}, nil
	}
	if passenger.Id == 0 {
		return &pb.LoginResp{
			Code:    401,
			Message: "用户未注册",
		}, nil
	}
	if result != req.SendSmsCode {
		return &pb.LoginResp{
			Code:    301,
			Message: "验证码错误",
		}, nil
	}
	return &pb.LoginResp{
		Code:    200,
		Message: "登录成功",
		PId:     int16(passenger.Id),
	}, nil
}

// UploadPassenger implements the PassengerServiceImpl interface.
func (s *PassengerServiceImpl) UploadPassenger(ctx context.Context, req *pb.UploadPassengerReq) (*pb.UploadPassengerResp, error) {
	// TODO: Your code here...
	var passenger model.Passenger
	if err := global.DB.Debug().Where("id = ?", req.Pid).Limit(1).Find(&passenger).Error; err != nil {
		return &pb.UploadPassengerResp{
			Code:    400,
			Message: "查询失败",
		}, nil
	}
	if passenger.Id == 0 {
		return &pb.UploadPassengerResp{
			Code:    401,
			Message: "未登录",
		}, nil
	}
	m := model.Passenger{
		Id:           int64(req.Pid),
		HandlerImage: int64(req.HandlerImage),
		Name:         req.Name,
		Sex:          req.Sex,
		Age:          int64(req.Age),
		Mobile:       req.Mobile,
	}
	if err := global.DB.Debug().Updates(&m).Error; err != nil {
		return &pb.UploadPassengerResp{
			Code:    503,
			Message: "服务器异常",
		}, nil
	}
	return &pb.UploadPassengerResp{
		Code:    200,
		Message: "更新成功",
	}, nil
}

// SartRating implements the PassengerServiceImpl interface.
func (s *PassengerServiceImpl) SartRating(ctx context.Context, req *pb.SartRatingReq) (*pb.SartRatingResp, error) {
	// TODO: Your code here...
	var passenger model.Passenger
	if err := global.DB.Debug().Where("id = ?", req.Pid).Limit(1).Find(&passenger).Error; err != nil {
		return &pb.SartRatingResp{
			Code:    400,
			Message: "查询失败",
		}, nil
	}
	if passenger.Id == 0 {
		return &pb.SartRatingResp{
			Code:    404,
			Message: "用户不存在",
		}, nil
	}
	rating := model.PStarRating{
		PId:        int64(req.Pid),
		StarRating: req.StarRating,
	}
	if err := global.DB.Debug().Updates(&rating).Error; err != nil {
		return &pb.SartRatingResp{
			Code:    503,
			Message: "服务器异常",
		}, nil
	}
	return &pb.SartRatingResp{
		Code:    200,
		Message: "评分成功",
	}, nil
}

// QueryPTrip implements the PassengerServiceImpl interface.
func (s *PassengerServiceImpl) QueryPTrip(ctx context.Context, req *pb.QueryPTripReq) (*pb.QueryPTripResp, error) {
	// TODO: Your code here...
	var passenger model.Passenger
	if err := global.DB.Debug().Where("id = ?", req.PId).Limit(1).Find(&passenger).Error; err != nil {
		return &pb.QueryPTripResp{
			Code:    503,
			Message: "查询失败",
		}, nil
	}
	if passenger.Id == 0 {
		return &pb.QueryPTripResp{
			Code:    404,
			Message: "用户不存在",
		}, nil
	}
	var trip model.PTrip
	if err := global.DB.Debug().Where("id = ?", req.PId).Limit(1).Find(&trip).Error; err != nil {
		return &pb.QueryPTripResp{
			Code:    400,
			Message: "查询失败",
		}, nil
	}
	marshal, _ := json.Marshal(trip)
	return &pb.QueryPTripResp{
		Code:    200,
		Message: "查询成功",
		Table:   string(marshal),
	}, nil
}
